/*
 * This file is part of the EasyFlash Library.
 *
 * Copyright (c) 2015, Armink, <armink.ztl@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * 'Software'), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Function: RT-Thread Finsh/MSH command for EasyFlash.
 * Created on: 2018-05-19
 */

#include <easyflash.h>
#include <rtthread.h>
#include <string.h>
#include <stdlib.h>

#if defined(RT_USING_FINSH) && defined(EF_USING_ENV)
static void env(uint8_t argc, char **argv)
{
    
#define CMD_SET_INDEX         0
#define CMD_GET_INDEX         1
#define CMD_PRINT_INDEX       2
#define CMD_RESET_INDEX       3
    
    const char* help_info[] =
    {
        [CMD_SET_INDEX]       = "env set <key> [value]    - EasyFlash Set an envrionment variable.",
        [CMD_GET_INDEX]       = "env get <key> [value]    - EasyFlash Get an envrionment variable by name.",
        [CMD_PRINT_INDEX]     = "env print                - EasyFlash Print all envrionment variables.",
        [CMD_RESET_INDEX]     = "env reset                - EasyFlash Reset all envrionment variable to default.",
    };
    if (argc < 2){
        rt_kprintf("Usage:\n");
        for (size_t i = 0; i < sizeof(help_info) / sizeof(char*); i++){
             rt_kprintf("%s\n", help_info[i]);
        }
        rt_kprintf("\n");
    
    } else {
        const char *operator = argv[1];
        if (!rt_strcmp(operator, "set")){
            if (argc > 4) {
                /* environment variable value string together */
                for (uint8_t i = 0; i < argc - 3; i++) {
                     argv[3 + i][rt_strlen(argv[3 + i])] = ' ';
                }
            }
            if (argc == 4) {
                ef_set_env(argv[2], argv[3]);
            } else {
                rt_kprintf("Please enter 'env set <key> [value]'.\n");
            }
        
        } else if (!rt_strcmp(operator, "get")){   
            char *value = NULL;
            value = ef_get_env(argv[2]);
            if (value) {
                rt_kprintf("The %s value is %s.\n", argv[2], value);
            } else {
                rt_kprintf("Please enter 'env get <key>'.\n");
            }
            
        } else if (!rt_strcmp(operator, "print")){
            ef_print_env();
            
        } else if (!rt_strcmp(operator, "reset")){
            ef_env_set_default();
            
        } else if (!rt_strcmp(operator, "hex")){
			uint32_t hex_32,hex_value;
			uint16_t hex_16;
			uint8_t hex_8;
			if (!rt_strcmp(argv[2], "get")){
				if (atoi(argv[4]) == 32){
					if (ef_get_env_blob(argv[3], &hex_32,sizeof(hex_32),RT_NULL) != 0){
						hex_value = hex_32;
					}
					
				} else if (atoi(argv[4]) == 16){
					if(ef_get_env_blob(argv[3], &hex_16,sizeof(hex_16),RT_NULL) != 0){
						hex_value = hex_16;
					}
				} else if (atoi(argv[4]) == 8){
					if (ef_get_env_blob(argv[3], &hex_8,sizeof(hex_8),RT_NULL) != 0){
						hex_value = hex_8;
					}
				}
				
				if (hex_value) {
					rt_kprintf("The %s value is %d.\n", argv[3], hex_value);
				} else {
					rt_kprintf("Please enter 'env hex get <key> 32/16/8 '.\n");
				}
			} else if (!rt_strcmp(argv[2], "set")){
				 EfErrCode result = EF_NO_ERR;
				if (atoi(argv[4]) == 32){
					hex_32 = atoi(argv[5]);
					result = ef_set_env_blob(argv[3], &hex_32, sizeof(uint32_t));
					hex_value = hex_32;
				} else if (atoi(argv[4]) == 16){
					hex_16 = atoi(argv[5]);
					result = ef_set_env_blob(argv[3], &hex_16, sizeof(uint16_t));
					hex_value = hex_16;
				} else if (atoi(argv[4]) == 8){
					hex_8 = atoi(argv[5]);
					result = ef_set_env_blob(argv[3], &hex_8, sizeof(uint8_t));
					hex_value = hex_8;
				} else {
					rt_kprintf("Please enter 'env hex set <key> 32/16/8 value'.\n");
				}
				if (result == EF_NO_ERR){
					if (hex_value) {
						rt_kprintf("The %s value is %d.\n", argv[3], hex_value);
					} else {
						rt_kprintf("value = 0.\n");
					}
				} else {
					rt_kprintf("get %s error",argv[2]);
				}
				
			}
		} 
		else {   
            rt_kprintf("Unknown command. Please enter 'env' for help.\n");
        }       
    }
}

MSH_CMD_EXPORT(env, EasyFlash operate.);
#endif /* defined(RT_USING_FINSH)*/
